﻿using Kingdee.BOS.Core;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using Kingdee.K3.Core.MFG.EntityHelper;
using Kingdee.K3.Core.MFG.EnumConst;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

namespace Jac.XkDemo.BOS.Business.PlugIn
{
    /// <summary>    
    /// 【单据转换插件】全事件    
    /// </summary>    
    [Description("【单据转换插件】全事件"), HotUpdate]
    public class AllEventConvertPlugIn : Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.AbstractConvertPlugIn
    {
        #region event        
        public override void OnInitVariable(Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args.InitVariableEventArgs e)
        {
            PrintEventInfo("OnInitVariable", e);
            base.OnInitVariable(e);
        }
        public override void OnQueryBuilderParemeter(Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args.QueryBuilderParemeterEventArgs e)
        {
            PrintEventInfo("OnQueryBuilderParemeter", e);
            base.OnQueryBuilderParemeter(e);
        }
        public override void OnBeforeGetSourceData(Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args.BeforeGetSourceDataEventArgs e)
        {
            PrintEventInfo("OnBeforeGetSourceData", e);
            base.OnBeforeGetSourceData(e);
        }
        public override void OnInSelectedRow(Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args.InSelectedRowEventArgs e)
        {
            PrintEventInfo("OnInSelectedRow", e);
            base.OnInSelectedRow(e);
        }
        public override void OnBeforeFieldMapping(Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args.BeforeFieldMappingEventArgs e)
        {
            PrintEventInfo("OnBeforeFieldMapping", e);
            base.OnBeforeFieldMapping(e);
        }
        public override void OnFieldMapping(Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args.FieldMappingEventArgs e)
        {
            PrintEventInfo("OnFieldMapping", e);
            base.OnFieldMapping(e);
        }
        public override void OnAfterFieldMapping(Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args.AfterFieldMappingEventArgs e)
        {
            PrintEventInfo("OnAfterFieldMapping", e);
            base.OnAfterFieldMapping(e);
        }
        public override void OnCreateLink(Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args.CreateLinkEventArgs e)
        {
            PrintEventInfo("OnCreateLink", e);
            base.OnCreateLink(e);
        }
        public override void OnSetLinkAmount(Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args.SetLinkAmountEventArgs e)
        {
            PrintEventInfo("OnSetLinkAmount", e);
            base.OnSetLinkAmount(e);
        }
        public override void OnAfterCreateLink(Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args.CreateLinkEventArgs e)
        {
            PrintEventInfo("OnAfterCreateLink", e);
            base.OnAfterCreateLink(e);
        }
        public override void OnGetSourceData(Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args.GetSourceDataEventArgs e)
        {
            PrintEventInfo("OnGetSourceData", e);
            base.OnGetSourceData(e);
        }
        public override void OnCreateTarget(Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args.CreateTargetEventArgs e)
        {
            PrintEventInfo("OnCreateTarget", e);
            base.OnCreateTarget(e);
        }
        public override void OnParseFilterOptions(Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args.ParseFilterOptionsEventArgs e)
        {
            PrintEventInfo("OnParseFilterOptions", e);
            base.OnParseFilterOptions(e);
        }
        public override void OnGetDrawSourceData(Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args.GetDrawSourceDataEventArgs e)
        {
            PrintEventInfo("OnGetDrawSourceData", e);
            base.OnGetDrawSourceData(e);
        }
        public override void OnCreateDrawTarget(Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args.CreateDrawTargetEventArgs e)
        {
            PrintEventInfo("OnCreateDrawTarget", e);
            base.OnCreateDrawTarget(e);
        }
        public override void OnBeforeGroupBy(Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args.BeforeGroupByEventArgs e)
        {
            PrintEventInfo("OnBeforeGroupBy", e);
            base.OnBeforeGroupBy(e);
        }
        public override void OnParseFilter(Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args.ParseFilterEventArgs e)
        {
            PrintEventInfo("OnParseFilter", e);
            base.OnParseFilter(e);
        }
        public override void OnGetConvertBusinessService(Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args.ConvertBusinessServiceEventArgs e)
        {
            PrintEventInfo("OnGetConvertBusinessService", e);
            base.OnGetConvertBusinessService(e);
        }
        public override void AfterConvert(Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args.AfterConvertEventArgs e)
        {
            PrintEventInfo("AfterConvert", e);
            base.AfterConvert(e);

            //下推单据只保留合格品
            ExtendedDataEntity[] objs = e.Result.FindByEntityKey("FBillHead"); //获取单据数据包 FBillHead是单据头标知
            foreach (ExtendedDataEntity obj in objs)
            {
                //找到生产入库单的表体数据
                DynamicObjectCollection entryDatas = obj.DataEntity.GetDynamicValue<DynamicObjectCollection>(Consts_PRD_InStock.Orm_Entry_Entity);
                List<DynamicObject> wEntryDatas = entryDatas.Where(w => w.GetDynamicValue<string>(Consts_PRD_InStock.Orm_Entry_InStockType) != "1").ToList();//找到入库单表体分录入库类型不是合格品入库数据
                foreach (DynamicObject entryData in wEntryDatas)
                {
                    //循环移除掉
                    entryDatas.Remove(entryData);
                }

            }
        }

        #endregion

        #region private method        
        /// <summary>        
        /// 事件执行次数计数器       
        /// </summary>        
        private int counter;
        /// <summary>       
        /// 事件执行次数计数器        
        /// </summary>        
        private int Counter { get { return ++counter; } }
        /// <summary>        
        /// 输出事件执行日志       
        /// </summary>        
        /// <param name="eventName">事件名称</param>        
        /// <param name="args">事件参数</param>        
        private void PrintEventInfo(string eventName, params object[] args)
        {
            var parameters = string.Empty;
            try
            {
                //parameters = Newtonsoft.Json.JsonConvert.SerializeObject(args);// 性能较差，可能会导致页面假死           
            }
            catch { }
            var log = string.Format("序号:{0},执行时间:{1},事件名称:{2},事件参数:{3}", Counter, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), eventName, parameters);
            System.Diagnostics.Debug.Print(log);
            Kingdee.BOS.Log.Logger.Error("EVENT", log, null);
        }
        #endregion
    }
}


